package com.ebpm.designer
{
	import com.ebpm.designer.dir.NodeContainerLTDir;
	import com.ebpm.designer.dir.NodeContainerRDDir;
	import com.ebpm.designer.item.DownItem;
	import com.ebpm.designer.item.Item;
	import com.ebpm.designer.item.LeftItem;
	import com.ebpm.designer.item.RightItem;
	import com.ebpm.designer.item.TopItem;
	import com.ebpm.designer.line.Line;
	import com.ebpm.designer.line.Point;
	import com.ebpm.designer.mainLayout.variable.Variable;
	import com.ebpm.designer.pvm.EndEvent;
	import com.ebpm.designer.pvm.ForkAfter;
	import com.ebpm.designer.pvm.ForkBefore;
	import com.ebpm.designer.pvm.ForkTask;
	import com.ebpm.designer.pvm.LaneAfter;
	import com.ebpm.designer.pvm.LaneBefore;
	import com.ebpm.designer.pvm.LaneForkTask;
	import com.ebpm.designer.pvm.LaneTask;
	import com.ebpm.designer.pvm.ServiceTask;
	import com.ebpm.designer.pvm.StartEvent;
	import com.ebpm.designer.pvm.SubEndEvent;
	import com.ebpm.designer.pvm.SubStartEvent;
	import com.ebpm.designer.pvm.UserTask;
	
	import mx.collections.ArrayList;
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.formatters.DateFormatter;

	public class NodeContainer extends Canvas
	{
		public static var ID_SQE:Number = 1;
		
		public var _id:String;
		public var type:String;
		public var mission:UserTask;
		public var workPanel:WorkPanel;
		
		/**移动***/
		public var startMoveX:Number;
		public var startMoveY:Number;
		public var isMoving:Boolean;																		
		/**移动**/
		
		/******改变大小********/
		public var resizeStartW:Number;
		public var resizeStartH:Number;
		public var isResizing:Boolean = false;
		//用来判断当前容器是否 已经添加了nw 和 se
		public var dirType:Number = 0;
		public var leftTopDir:NodeContainerLTDir;
		public var rightDownDir:NodeContainerRDDir;
		/******改变大小*******/
		
		/**线点***/
		//标志是否正在连线，不然会跟move的事件出现冲突，在连线不能move
		public var leftItem:LeftItem;
		public var rightItem:RightItem;
		public var topItem:TopItem;
		public var downItem:DownItem;
		public var items:ArrayList = new ArrayList();
		/**线点***/
		
		public var variable:Variable = new Variable();
		
		public function NodeContainer(_x:Number, _y:Number, wp:WorkPanel, type:String="UserTask", w:Number=0, h:Number=0) 
		{
			//1、判断是否重复【开始节点】和【结束节点】
			if((WF.wp.startEvent != null && type == "StartEvent") || (WF.wp.endEvent != null && type == "EndEvent")) {
				Alert.show("已存在【开始节点】和【结束节点】");
				return ;
			}
			
			//2、设置基本参数
			this.workPanel = wp;
			
			var myDateFormatter:DateFormatter=new DateFormatter();
			myDateFormatter.formatString="YYMMDDHHNNSS";
			var dateId:String = myDateFormatter.format(new Date());

			this.variable.name = type + dateId + WF.getId();
			this._id = "utContainer_" + ID_SQE;
			ID_SQE = ID_SQE + 1;
			this.type = type;
			this.x = _x;
			this.y = _y;
			this.width = WF.MIN_NODE_W;
			this.height = WF.MIN_NODE_H;
			if(w!=0) {
				this.width = w;
				this.height = h;
			}
			
			//3、根据类型，初始化Mission
			if(this.type == "UserTask") {
				mission = new UserTask(this);
			} else if(this.type == "StartEvent") {
				mission = new StartEvent(this);
				this.workPanel.startEvent = mission;
			} else if(this.type == "EndEvent") {
				mission = new EndEvent(this);
				this.workPanel.endEvent = mission;
			} else if(this.type == "SubEndEvent") {
				mission = new SubEndEvent(this);
			} else if(this.type == "ServiceTask") {
				mission = new ServiceTask(this);
			} else if(this.type == "ForkTask") {
				mission = new ForkTask(this);
			} else if(this.type == "LaneTask") {
				mission = new LaneTask(this);
			} else if(this.type == "LaneForkTask") {
				mission = new LaneForkTask(this);
			} else if(this.type == "ForkBefore") {
				mission = new ForkBefore(this);
			} else if(this.type == "ForkAfter") {
				mission = new ForkAfter(this);
			} else if(this.type == "SubStartEvent") {
				mission = new SubStartEvent(this);
			} else if(this.type == "LaneBefore") {
				mission = new LaneBefore(this);
			} else if(this.type == "LaneAfter") {
				mission = new LaneAfter(this);
			}
			
//			this.setStyle("backgroundColor", "#333333");																				//设置背景颜色
//			this.setStyle("backgroundAlpha", 0.2);																								//设置背景色透明度
			
			this.addElement(mission);
			initItemsAndDirs();																																	//初始化四个【连线点】和【变化框】
			
			this.workPanel.nodeContainers.addItem(this);
			this.workPanel.addElement(this);
		}
		
		private function initItemsAndDirs():void {																							//初始化【连线点】和【变化框】		
			leftTopDir = new NodeContainerLTDir(this);
			rightDownDir = new NodeContainerRDDir(this);
			this.addElement(leftTopDir);
			this.addElement(rightDownDir);
			leftTopDir.setVisible(false);
			rightDownDir.setVisible(false);
			
			leftItem = new LeftItem(this);
			rightItem = new RightItem(this);
			topItem = new TopItem(this);
			downItem = new DownItem(this);
			items.addItem(leftItem);
			items.addItem(rightItem);
			items.addItem(topItem);
			items.addItem(downItem);
			this.addElement(leftItem);
			this.addElement(rightItem);
			this.addElement(topItem);
			this.addElement(downItem);
			leftItem.setVisible(false);
			rightItem.setVisible(false);
			topItem.setVisible(false);
			downItem.setVisible(false);
		}
		
		//将线的颜色设置为default 颜色
		public function setLinesDefaultColor():void {
			for(var i:int=0;i<items.length;i++) {
				var item:Item = this.items.getItemAt(i) as Item;
				item.setLinesToDefault();
			}
		}
		
		public function moving():void
		{
			if(this.isMoving && !this.mission.isEditing) 
			{
				var x:Number = this.startMoveX + WP.mouseChangeX;
				var y:Number = this.startMoveY + WP.mouseChangeY;
				if(this.isOverOtherContainer(x, y, this.width, this.height)) return ;
				this.x = x;
				this.y = y;
				moveLines();
			}
			
		}
		
		public function movingBySelectRect(changeX:Number, changeY:Number):void {
			this.x = this.startMoveX + changeX;
			this.y = this.startMoveY + changeY;
			moveLines();
		}
		
		public function movingByKeyBoard(changeX:Number=0, changeY:Number=0):void {
			if(WP.isMovingNodeContainer && !this.mission.isEditing) 
			{
				this.x = this.x + changeX;
				this.y = this.y + changeY;
				this.moveLines();
			}
		}
		
		/**
		 * 移动【NodeContainer - items】 的【sourceLines】和【targetLines】
		 * @注意：【NodeContainer】有【sourceLines】和【targetLines】 - 两种线都必须移动
		 **/
		public function moveLines():void {																						
			var i:int = 0;
			var k:int = 0;
			var item:Item = null;
			var line:Line = null;
			for(i=0; i<items.length; i++) 
			{
				item = items.getItemAt(i) as Item;
				
				for(k=0; k<item.sourceLines.length; k++)  																		//移动fromLines
				{
					line = item.sourceLines.getItemAt(k) as Line;
					if(WP.isMovingSelectRectByKeyboard) {																//如果是移动【SelectRect】
						line.paint(line.sourceItem.getPageX(), line.sourceItem.getPageY(), line.targetItem.getPageX(), line.targetItem.getPageY());						
					} else {																																//如果是移动单个【NodeContainer】
						line.repaintLine("from");
					}
					line.movingTxtByLine();
				}
				
				for(k=0; k<item.targetLines.length; k++) 																			//移动toLines
				{
					line = item.targetLines.getItemAt(k) as Line;
					if(WP.isMovingSelectRectByKeyboard) {
						line.paint(line.sourceItem.getPageX(), line.sourceItem.getPageY(), line.targetItem.getPageX(), line.targetItem.getPageY());					
					} else {
						line.repaintLine();
					}
					line.movingTxtByLine();
				}
				
			}
		}
		
		public function buildLineXml():Boolean {
			var i:int = 0;
			var k:int = 0;
			var item:Item;
			var line:Line;
			for(i=0; i<items.length; i++) {
				item = items.getItemAt(i) as Item;
				//移动fromLines
				for(k=0; k<item.sourceLines.length; k++) {
					line = item.sourceLines.getItemAt(k) as Line;
					WF.xmlDefinitionsData += "\t<SequenceFlow id=\"" + line.variable.name + "\"";
					if(line.variable.desc != null && line.variable.desc != "") {
						WF.xmlDefinitionsData += " desc=\"" + line.variable.desc + "\"";
					}
					WF.xmlDefinitionsData += 
						" sourceRef=\"" + line.sourceItem.nodeContainer.variable.name + "\"" +
						" targetRef=\"" + line.targetItem.nodeContainer.variable.name + "\"" +
						" >\n";
					WF.xmlDefinitionsData += "\t\t<Express><![CDATA[";
					if(line.variable.express != null && line.variable.express != "") {
						WF.xmlDefinitionsData += line.variable.express;
					}
					WF.xmlDefinitionsData += "]]></Express>\n";
					WF.xmlDefinitionsData += "\t</SequenceFlow>\n";
					
					var pointXml:String = "{\"type\":\"" + line.className + "\",\"name\":\"" + line.variable.name + "\",";
					for(var kk:Number=0; kk<line.points.length; kk++) {
						var p:Point = line.points.getItemAt(kk) as Point;
						pointXml += "\"x" + (kk+1) + "\":" + p.x + ",\"y" + (kk+1) + "\":" +  p.y;
						if(kk != (line.points.length-1)) {
							pointXml += ",";
						}
					}
					pointXml += ",\"desc\":\"" + (line.variable.desc==null?"":line.variable.desc) + "\",\"lx\":" + line.txtLabel.x + ",\"ly\":" + line.txtLabel.y + "}";
					WF.xmlLineJson += pointXml + ",";
					//Alert.show("" + pointXml);
					
					WF.xmlPositionData += "\t<SequenceFlow" +
						" id=\"" + line.variable.name + "\"" +
						" type=\"" + line.className + "\"" +
						" resizeFromExtraX=\"" + line.resizeFromExtraX + "\"" +
						" resizeFromExtraY=\"" + line.resizeFromExtraY + "\"" +
						" resizeToExtraX=\"" + line.resizeToExtraX + "\"" +
						" resizeToExtraY=\"" + line.resizeToExtraY + "\"" +
						" resizeMiddleExtra=\"" + line.resizeMiddleExtra + "\"" +
						" distanceWidth=\"" + line.distanceWithEndPointWidth + "\"" +
						" distanceHeight=\"" + line.distanceWithEndPointHeight + "\"" +
						//" json=\"" + pointXml + "\"" +
						" />\n";
					
					
					
				}
			}
			return true;
		}
		
		//判断是否覆盖到workpanel其他userTaskContainer，如果是，不允许移动
		public function isOverOtherContainer(x:Number, y:Number, width:Number, height:Number):Boolean {
			for(var i:int=0; i<this.workPanel.nodeContainers.length; i++) {
				var container:NodeContainer = this.workPanel.nodeContainers.getItemAt(i) as NodeContainer;
				if(container._id == this._id) continue;
				if(WF.intersects(x, y, this.width, this.height, container.x, container.y, container.width, container.height)) {
					return true;
				}
			}
			return false;
		}
		
		//推拽事件
		public function resizing():void {
			if(this.isResizing) 
			{
				if(this.dirType != 1 && this.dirType != 2) return ;
				var changeW:Number = WP.mouseChangeX;
				var changeH:Number = WP.mouseChangeY;
				//如果移动右下角
				if(this.dirType == 2) {
					if(this.isOverOtherContainer(this.x, this.y, this.resizeStartW + changeW, this.resizeStartH + changeH)) {
						this.x = this.x - 1;
						this.y = this.y - 1;
						return ;
					}
					this.width = this.resizeStartW + changeW;
					this.height = this.resizeStartH + changeH;
				} 
				//如果移动左上角
				else {
					var moveX:Number = this.startMoveX + changeW;
					var moveY:Number = this.startMoveY + changeH;
					if(this.isOverOtherContainer(moveX, moveY, this.resizeStartW + changeW, this.resizeStartH + changeH)) {
						this.x = this.x + 1;
						this.y = this.y + 1;
						return ;
					}
					this.width = this.resizeStartW - changeW;
					this.height = this.resizeStartH - changeH;
					this.x = moveX;
					this.y = moveY;
				}
				
				//处理完自身推拽事件以后，处理子容器和线的大小位置
				resizingChildren();
			}
		}
		
		public function resizingChildren():void {
			moveLines();
			this.mission.resizingByParent();
			this.leftItem.resizeByNodeContainer();
			this.rightItem.resizeByNodeContainer();
			this.topItem.resizeByNodeContainer();
			this.downItem.resizeByNodeContainer();
			this.rightDownDir.resizingByParent();
		}
		
		public function deleteMySelf():void {
			this.workPanel.removeElement(this);
			this.workPanel.nodeContainers.removeItem(this);
			var i:int = 0;
			var k:int = 0;
			var item:Item;
			var line:Line;
			var tempLines:ArrayList = new ArrayList();
			for(i=0; i<items.length; i++) {
				item = items.getItemAt(i) as Item;
				for(k=0; k<item.targetLines.length; k++) {
					line = item.targetLines.getItemAt(k) as Line;
					tempLines.addItem(line);
				}
			}
			
			for(i=0; i<items.length; i++) {
				item = items.getItemAt(i) as Item;
				for(k=0; k<item.sourceLines.length; k++) {
					line = item.sourceLines.getItemAt(k) as Line;
					tempLines.addItem(line);
				}
			}
			for(k=0; k<tempLines.length; k++) {
				line = tempLines.getItemAt(k) as Line;
				line.deleteMyself();
			}
		}
		
		public function getRightLineX():Number {
			return this.x + this.width - WF.ITEM_SIZE - WF.ITEM_SIZE / 2;
		}
		
		public function getRightLineY():Number {
			return this.y + this.height / 2 - WF.ITEM_SIZE / 2;
		}
		
		public function getDownLineX():Number {
			return this.x + this.width / 2 - WF.ITEM_SIZE / 2;
		}
		
		public function getDownLineY():Number {
			return this.y + this.height - WF.ITEM_SIZE - WF.ITEM_SIZE / 2;
		}
		
		public function getTopLineX():Number {
			return this.x + this.width / 2 - WF.ITEM_SIZE / 2;
		}
		
		public function getTopLineY():Number {
			return this.y + WF.ITEM_SIZE / 2;
		}
		
		public function getLeftLineX():Number {
			return this.x +  WF.ITEM_SIZE / 2;
		}
		
		public function getLeftLineY():Number {
			return this.y + this.height / 2 - WF.ITEM_SIZE / 2;
		}
		
		public function addDirs():void {
			leftTopDir.setVisible(true);
			rightDownDir.setVisible(true);
		}
		
		public function removeDirs():void {
			leftTopDir.setVisible(false);
			rightDownDir.setVisible(false);
		}
		
		public function displayItems():void {
			for(var i:int=0; i<this.items.length; i++) {
				var item:Item = this.items.getItemAt(i) as Item;
				item.setVisible(true);
			}
		}
		
		public function hideItems():void {
			for(var i:int=0; i<this.items.length; i++) {
				var item:Item = this.items.getItemAt(i) as Item;
				item.setVisible(false);
			}
		}
		
		public function isCover(mouseX:Number, mouseY:Number):Boolean {
			if(mouseX > this.x && mouseX < (this.x + this.width) && mouseY > this.y && mouseY < (this.y + this.height)) {
				return true
			}
			return false;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}